﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[DefaultProperty("Group")]
	public class MyCheckBoxField : System.Web.UI.WebControls.BoundField {
		private bool _suppressPropertyThrows;

		/// <summary>
		/// Name of checkboxes.
		/// </summary>
		[Bindable(true)]
		[Category("Accessibility")]
		[DefaultValue("")]
		public string Group { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public MyCheckBoxField() {
			Group = "__";
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="newField"></param>
		protected override void CopyProperties(DataControlField newField) {
			_suppressPropertyThrows = true;
			base.CopyProperties(newField);
			(newField as MyCheckBoxField).Group = Group;
			_suppressPropertyThrows = false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override System.Web.UI.WebControls.DataControlField CreateField() {
			return new MyCheckBoxField();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="dictionary"></param>
		/// <param name="cell"></param>
		/// <param name="rowState"></param>
		/// <param name="includeReadOnly"></param>
		public override void ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) {
			Control control = null;
			string dataField = this.DataField;
			object dataValue = null;
			if (cell.Controls.Count > 0) {
				control = cell.Controls[0];
				CheckBox box = control as CheckBox;
				if ((box != null) && (includeReadOnly || box.Enabled)) {
					dataValue = box.Checked;
				}
			}
			if (dataValue != null) {
				if (dictionary.Contains(dataField)) {
					dictionary[dataField] = dataValue;
				} else {
					dictionary.Add(dataField, dataValue);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override object GetDesignTimeValue() {
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cell"></param>
		/// <param name="cellType"></param>
		/// <param name="rowState"></param>
		/// <param name="rowIndex"></param>
		public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) {
			if (cellType == DataControlCellType.Header) {
				var box = new MyInputControl("checkbox");
				box.Name = Group + "_all";
				box.Attributes["onclick"] = "return groupCheckBoxes(\"" + box.Name + "\");";
				cell.Controls.Add(box);
			} else if (cellType == DataControlCellType.DataCell) {
				this.InitializeDataCell(cell, rowState);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="cell"></param>
		/// <param name="rowState"></param>
		protected override void InitializeDataCell(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlRowState rowState) {
			if (this.DataField.Length != 0) {
				var checkbox = new MyInputControl("checkbox");

				checkbox.Name = Group;
				cell.Controls.Add(checkbox);

				if (base.Visible) {
					checkbox.DataBinding += new EventHandler(this.OnDataBindField);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected override void OnDataBindField(object sender, EventArgs e) {
			Control control = (Control)sender;
			Control namingContainer = control.NamingContainer;
			object dataValue = GetValue(namingContainer);
			if (!(control is MyInputControl)) {
				throw new HttpException("Wrong type of control.");
			}
			if (dataValue == null || dataValue == DBNull.Value) {
				((MyInputControl)control).Value = string.Empty;
			} else if (dataValue is string) {
				((MyInputControl)control).Value = (string)dataValue;
			} else {
				((MyInputControl)control).Value = dataValue.ToString();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override bool ApplyFormatInEditMode {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"ApplyFormatInEditMode\" is not supported.");
				}
				return false;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"ApplyFormatInEditMode\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override bool ConvertEmptyStringToNull {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"ConvertEmptyStringToNull\" is not supported.");
				}
				return false;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"ConvertEmptyStringToNull\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[TypeConverter(typeof(System.Web.UI.Design.DataSourceBooleanViewSchemaConverter))]
		public override string DataField {
			get {
				return base.DataField;
			}
			set {
				base.DataField = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override string DataFormatString {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"DataFormatString\" is not supported.");
				}
				return string.Empty;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"DataFormatString\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override bool HtmlEncode {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"HtmlEncode\" is not supported.");
				}
				return false;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"HtmlEncode\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override bool HtmlEncodeFormatString {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"HtmlEncodeFormatString\" is not supported.");
				}
				return false;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"HtmlEncodeFormatString\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public override string NullDisplayText {
			get {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"NullDisplayText\" is not supported.");
				}
				return string.Empty;
			}
			set {
				if (!this._suppressPropertyThrows) {
					throw new NotSupportedException("Property \"NullDisplayText\" is not supported.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected override bool SupportsHtmlEncode {
			get {
				return false;
			}
		}

	} // end of MyCheckBoxField.
}
